package org.example.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.example.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProduceDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.建立连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.PORT);
        connectionFactory.setUsername(Constants.USER_NAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL_HOST);
        //2.建立连接
        Connection connection =connectionFactory.newConnection();

        //3.创建信道
        Channel channel = connection.createChannel();

        //4.声明队列
        /**
         * * queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
         *          *                                  Map<String, Object> arguments)
         *   queue:队列名称
         *   durable:是否持久化
         *   exclusive:是否独占
         *   autoDelete:是否自动删除
         *   arguments:参数配置
         */
        /**
         * basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body)
         * exchange:交换机的名称
         * routingKey:关键key，路由键
         * props：参数配置
         * body：发送的信息
         */
        channel.queueDeclare("hello",true,false,false,null);
        for(int i=0;i<Constants.MESSAGE_COUNT;i++) {
            //5.开始发送消息
            //使用内置的交换机，
            String msg = "hello rabbitmq~"+i;
            channel.basicPublish("","hello",null,msg.getBytes());
        }
        System.out.println("发送信息成功");
        //6.释放资源
        //这是一个进程，不关闭文件的话，就会一直运行下去
        channel.close();
        connection.close();
    }
}
